﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Diagnostics;
using System.Data;

namespace MyClever.Lib
{
    public class RemotePluginReflector : MarshalByRefObject
    {
        public List<KeyValuePair<string, string>> ExtractPlugins(PluginPath pluginPath, ProgrammingLanguage programmingLanguage)
        {
            List<KeyValuePair<string, string>> pluginNames = new List<KeyValuePair<string, string>>();
            
            // today we need plugin reflection for c# assemblys only!!
            Debug.Assert(ProgrammingLanguage.CSharp == programmingLanguage);

            Assembly pluginAssembly = Assembly.Load(pluginPath.Name);
            foreach (Type pluginType in pluginAssembly.GetTypes())
            {
                //Only look at public types
                if (pluginType.IsPublic)
                {
                    //Only look at non-abstract types
                    if (!pluginType.IsAbstract)
                    {
                        //Gets a type object of the interface we need the plugins to match
                        Type typeInterface = pluginType.GetInterface("MyClever.Lib.IPlugin", true);

                        //Make sure the interface we want to use actually exists
                        if (typeInterface != null)
                        {
                            pluginNames.Add(new KeyValuePair<string, string>(pluginType.Name, pluginType.FullName));
                        }
                        typeInterface = null;
                    }
                }
            }
            return pluginNames;
        }
    }
}
